package cn.wolfcode.controller;


import cn.wolfcode.annotation.RequireLogin;
import cn.wolfcode.annotation.UserParam;
import cn.wolfcode.entity.*;
import cn.wolfcode.mongo.domain.StrategyComment;
import cn.wolfcode.mongo.query.StrategyCommentQuery;
import cn.wolfcode.mongo.service.IStrategyCommentMongoService;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.redis.vo.StrategyStatisVO;
import cn.wolfcode.service.*;
import cn.wolfcode.util.JSONResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@RestController
@CrossOrigin
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    IStrategyService strategyService;

    @Autowired
    IStrategyContentService strategyContentService;
    @Autowired
    IStrategyConditionService strategyConditionService;

    @Autowired
    IStrategyThemeService themeService;

    @Autowired
    IStrategyCommentMongoService strategyCommentMongoService;

    @GetMapping("/query")
    public JSONResult query(StrategyQuery qo) {
        Page<Strategy> page = strategyService.selectPage(qo);
        return JSONResult.success(page);
    }

    @Autowired
    private IStrategyRankService rankService;

    @GetMapping("/rank")
    public JSONResult rank(int type) {
        List<StrategyRank> list = rankService.queryRank(type);
        return JSONResult.success(list);
    }

    @GetMapping("/condition")
    public JSONResult condition(int type) {
        List<StrategyCondition> list = strategyConditionService.queryCondition(type);
        return JSONResult.success(list);
    }

    @GetMapping("/detail")
    public JSONResult detail(Long id) {
        Strategy strategy = strategyService.getById(id);
        strategy.setContent(strategyContentService.getById(strategy.getId()));
        //TODO viewnum++
        strategyStatisVORedisService.viewnumIncrease(strategy.getId());
        return JSONResult.success(strategy);
    }

    @GetMapping("/themeCds")
    public JSONResult themeCds() {
        List<Map<String, Object>> res = strategyService.queryHotTheme();
        return JSONResult.success(res);
    }

    @GetMapping("themes")
    public JSONResult themes() {
        List<StrategyTheme> list = themeService.list();
        return JSONResult.success(list);

    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public JSONResult commentAdd(StrategyComment comment, @UserParam Userinfo user) {
        strategyCommentMongoService.save(comment, user);
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId());
        return JSONResult.success();
    }

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/comments")
    public JSONResult comments(StrategyCommentQuery qo) {
        var page = strategyCommentMongoService.queryPage(qo);
        return JSONResult.success(page);
    }

    @GetMapping("/statisVo")
    public JSONResult statisVo(Long sid) {
        StrategyStatisVO vo = strategyStatisVORedisService.getStrategyVO(sid);
        return JSONResult.success(vo);
    }

    @RequireLogin
    @PostMapping("/favor")
    public JSONResult favor(Long sid, @UserParam Userinfo user) {
        boolean b = strategyStatisVORedisService.favor(sid, user.getId());
        return JSONResult.success(b);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public JSONResult thumbUp(Long sid, @UserParam Userinfo user) {
        boolean b = strategyStatisVORedisService.strategyThumbup(sid, user.getId());
        return JSONResult.success(b);
    }


}

